import Vue from "vue";
import store from '@/store'
import { Message } from "element-ui";
import router from "../router";
import $axios from "axios";
import $qs from "qs";
import { VUE_APP_BASE_API } from '../../public/config'

const $http = $axios.create({
  timeout: 60000,
  headers: { "Content-Type": "application/json" },
});


let BASE_URL = ''

BASE_URL = VUE_APP_BASE_API;
$http.BASE_URL = VUE_APP_BASE_API;
/**
 * 请求拦截
 */
$http.interceptors.request.use(
  (config) => {
    // 请求地址拦截
    // if (!config.url.startsWith('http://') &&
    //     !config.url.startsWith('https://') &&
    //     !config.url.startsWith('/database') &&
    //     !config.url.startsWith('/gencode') &&
    //     !config.url.startsWith('/sys/menu/treeData') &&
    //     !config.url.startsWith('/sys/dict/type/list') &&
    //     !config.url.startsWith('/database/datalink/dataSource/treeData')
    // ) {
    //     return Promise.reject(new Error('请求地址必须以http或https开头: ' + config.url))
    // }
    // config.url = '/api' + config.url
    if (localStorage.getItem("token")) {
      config.headers.token = localStorage.getItem("token");
    }
    config.url = BASE_URL + config.url;
    const type = config.method;
    const arrayFormat = config.headers.arrayFormat || "indices";
    if (
      type === "post" &&
      config.headers["Content-Type"] ===
        "application/x-www-form-urlencoded; charset=utf-8"
    ) {
      // post请求参数处理
      config.data = $qs.stringify(config.data, {
        allowDots: true,
        arrayFormat: arrayFormat,
      });
    } else if (type === "get") {
      // get请求参数处理
      config.paramsSerializer = (params) => {
        return $qs.stringify(params, {
          allowDots: true,
          arrayFormat: arrayFormat,
        });
      };
    }
    if (config.url.includes("/apiknowledgeData/save")) {
      config.headers["Content-Type"] = "multipart/form-data";
      config.data = config.data;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

/**
 * 响应拦截
 */
$http.interceptors.response.use(
  (response) => {
    if (response.data.code == -1) {
      store.dispatch('tagsView/delAllViews');
      localStorage.clear();
      router.push("/login");
    }
    return response;
  },
  (error) => {
    if (error.response) {
      if (error.response.status === 408 || error.response.status === 401) {
        // 需要重新登录
        Message({
          message: error.response.data,
          type: "error",
          showClose: true,
          dangerouslyUseHTMLString: true,
          duration: 3000,
        });
      } else if (error.response.status === 404) {
        // 路径找不到
        Message({
          message: "404 路径找不到" + ": " + error.response.config.url,
          type: "error",
          showClose: true,
          duration: 3000,
        });
      } else if (error.response.status === 503) {
        Message({
          message: "503 服务不可用" + ": " + error.response.config.url,
          type: "error",
          showClose: true,
          dangerouslyUseHTMLString: true,
          duration: 3000,
        });
      } else if (error.response.status === 504) {
        Message({
          message: "504 网络连接错误" + ": " + error.response.data,
          type: "error",
          showClose: true,
          dangerouslyUseHTMLString: true,
          duration: 3000,
        });
      } else {
        Message({
          message: error.response.data || error.response,
          type: "error",
          showClose: true,
          dangerouslyUseHTMLString: true,
          duration: 5000,
        });
      }
    } else {
      Message({
        message: error,
        type: "error",
        showClose: true,
        dangerouslyUseHTMLString: true,
        duration: 5000,
      });
    }

    return Promise.reject(error);
  }
);

export default $http;
